﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC
{
	public partial class ConnectTerminalForm : System.Windows.Forms.Form
	{
		public bool BeAutoCancel { get; set; }

		public ConnectTerminalForm.ConnStyle TerminalConn { get; set; }

		public ConnectTerminalForm()
		{
			this.InitializeComponent();
			List<KeyValuePair<Control, bool>> inputControlGroup = new List<KeyValuePair<Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{E9C81114-4ECD-431F-B84C-4653E5DE12B3}", null);
		}

		private void ConnectTerminalForm_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new Point(230, 170);
			}
			//base.Icon = Resource.YArchitechIcon;
			this.InitialButton();
		}

		private void InitialButton()
		{
			string ductConnTerminalStyle = this.userSetting.GetDuctConnTerminalStyle();
			if (ductConnTerminalStyle == "Elbow")
			{
				this.SelectButton(this.btnElbow);
				return;
			}
			if (ductConnTerminalStyle == "TakeOff")
			{
				this.SelectButton(this.btnTakeOff);
				return;
			}
			if (ductConnTerminalStyle == "Tee")
			{
				this.SelectButton(this.btnTee);
				return;
			}
			if (ductConnTerminalStyle == "Cross")
			{
				this.SelectButton(this.btnCross);
				return;
			}
			if (!(ductConnTerminalStyle == "OnDuct"))
			{
				return;
			}
			this.SelectButton(this.btn0nDuct);
		}

		private void SelectButton(Button button)
		{
			button.PerformClick();
			button.Select();
			button.Focus();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void ConnectTerminalForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.ChangeOperateState();
			this.ChangeOperateState();
		}

		private void ConnectTerminalForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void ConnectTerminalForm_Leave(object sender, EventArgs e)
		{
		}

		private void ConnectTerminalForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				ConnectTerminalForm.SendMessage(HYDuctConnectAirrTerminal.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void ConnectTerminalForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
			this.userSetting.WriteDuctConnTerminalStyle(this.TerminalConn.ToString());
		}

		private void btnElbow_Click(object sender, EventArgs e)
		{
			Button button = sender as Button;
			Color backColor = this.groupBox1.BackColor;
			string name = button.Name;
			if (!(name == "btnElbow"))
			{
				if (!(name == "btnTakeOff"))
				{
					if (!(name == "btnTee"))
					{
						if (!(name == "btnCross"))
						{
							if (name == "btn0nDuct")
							{
								this.TerminalConn = ConnectTerminalForm.ConnStyle.OnDuct;
							}
						}
						else
						{
							this.TerminalConn = ConnectTerminalForm.ConnStyle.Cross;
						}
					}
					else
					{
						this.TerminalConn = ConnectTerminalForm.ConnStyle.Tee;
					}
				}
				else
				{
					this.TerminalConn = ConnectTerminalForm.ConnStyle.TakeOff;
				}
			}
			else
			{
				this.TerminalConn = ConnectTerminalForm.ConnStyle.Elbow;
			}
			foreach (object obj in this.groupBox1.Controls)
			{
				Button button2 = obj as Button;
				button2.BackColor = backColor;
				if (button2 == button)
				{
					button2.BackColor = Color.LightSkyBlue;
				}
			}
			button.Focus();
			this.ChangeOperateState();
		}

		private void ChangeOperateState()
		{
			Cursor.Position = new Point(Cursor.Position.X, base.Bottom + 1);
			this.SendSetfocusMessage();
			this.SimulationPressESC();
			this.BeAutoCancel = true;
		}

		private void SimulationPressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			ConnectTerminalForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			ConnectTerminalForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void SendSetfocusMessage()
		{
			int wMsg = 7;
			ConnectTerminalForm.SendMessage(HYDuctConnectAirrTerminal.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		private HVACUserSetting userSetting = new HVACUserSetting();

		private ControlInfoManager m_thisManger;

		public enum ConnStyle
		{
			Elbow,
			TakeOff,
			Tee,
			Cross,
			OnDuct
		}
	}
}
